﻿//---------------------------------------------------------------------------
//
// Copyright (C) Inter Access B.V..  All rights reserved.
//
//---------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Schedule.Primitives;


namespace Schedule
{
    public class ScheduleFlatItemPresenter : ItemsControl
    {
        static ScheduleFlatItemPresenter()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ScheduleFlatItemPresenter), new FrameworkPropertyMetadata(typeof(ScheduleFlatItemPresenter)));
        }

        #region Overrides

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            // Only TimeItems can be presented without wrapping
            return item is TimeItem;
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            // return a new container for the object
            return new TimeItem();
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            // TODO: Bind to property user has described

            // If it's not a timeitem, we need to add some bindings
            if (!(item is TimeItem))
            {
                TimeItem typedItem = (TimeItem)element;

                // Start binding
                Binding startBinding = new Binding();
                startBinding.Path = new PropertyPath("Start");

                // End binding
                Binding endBinding = new Binding();
                endBinding.Path = new PropertyPath("End");

                // Add the bindings
                typedItem.SetBinding(TimePanel.ItemStartProperty, startBinding);
                typedItem.SetBinding(TimePanel.ItemEndProperty, endBinding);
            }

            // Call the base method
            base.PrepareContainerForItemOverride(element, item);
        }

        #endregion

    }
}
